/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package camel.server.session;

import camel.client.ClientEvent;

import java.io.Serializable;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Lob;
import javax.persistence.Column;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

/**
 *
 * @author mac
 */
//@IdClass(SessionCommandKey.class)
@Entity
//@NamedQuery(name = "findNotMyCommandInThisSession", query = "SELECT c FROM SessionCommand c WHERE c.masterSessionId = :sessionId AND c.id > :id AND c.originator <> :me")
@NamedQueries ( {
	@NamedQuery(name = "findAllCommandsInThisSession", query = "SELECT c FROM SessionCommand c WHERE c.masterSession = :masterSession AND c.id > :id"),
	@NamedQuery(name = "findNotMyCommandsInThisSession", query = "SELECT c FROM SessionCommand c WHERE c.masterSession = :masterSession AND c.id > :id AND c.originator <> :me ORDER BY id")
})
public class SessionCommand implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    
    private CollaborationSession masterSession;
    //private Long masterSessionId;
    
    // ClientEvent seems to be too small, so hibernate O-R mapping always uses tinyblob type
    // however, tinyblob is too small for the all other events
    // @Lob @Column(name = "hah", length=1024000) // Mac:nothing works!
    private Object cmd;
    
    private String originator;
    
    private String specificTarget;

    public void setId(Long id) {
        this.id = id;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof SessionCommand)) {
            return false;
        }
        SessionCommand other = (SessionCommand) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "camel.server.session.SessionCommand[id=" + id + "]";
    }

    @ManyToOne
    public CollaborationSession getMasterSession() {
        return masterSession;
    }

    public void setMasterSession(CollaborationSession session) {
        this.masterSession = session;
        //this.masterSessionId = session.getId();
    }

    public ClientEvent getCmd() {
        return (ClientEvent)cmd;
    }

    public void setCmd(ClientEvent cmd) {
        this.cmd = cmd;
    }

    public String getOriginator() {
        return originator;
    }

    public void setOriginator(String originator) {
        this.originator = originator;
    }
/*
    //@Id
    public Long getMasterSessionId() {
        return masterSessionId;
    }

    public void setMasterSessionId(Long masterSessionId) {
        this.masterSessionId = masterSessionId;
    }
*/

	public void setSpecificTarget(String specificTarget) {
		this.specificTarget = specificTarget;
	}

	public String getSpecificTarget() {
		return specificTarget;
	}

}
